package com.muyu.user.controller;

import java.util.List;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.log.annotation.Log;
import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.muyu.user.domain.SysUserLevel;
import com.muyu.user.domain.req.SysUserLevelQueryReq;
import com.muyu.user.domain.model.SysUserLevelQueryModel;
import com.muyu.user.domain.model.SysUserLevelPageQueryModel;
import com.muyu.user.domain.resp.SysUserLevelPageResp;
import com.muyu.user.domain.req.SysUserLevelSaveReq;
import com.muyu.user.domain.req.SysUserLevelEditReq;
import com.muyu.user.domain.model.SysUserLevelSaveModel;
import com.muyu.user.domain.model.SysUserLevelEditModel;
import com.muyu.user.service.SysUserLevelService;
import com.muyu.common.core.web.page.DataPageResp;
import com.muyu.common.core.web.page.PageQueryModel;

/**
 * 用户等级Controller
 *
 * @author liuguoqiang
 * @date 2025-09-14
 */
@RestController
@RequestMapping("/level")
@Tag(name = "用户等级", description = "用户等级控制层")
public class SysUserLevelController extends BaseController {

    /**
    * 用户等级业务层
    */
    @Autowired
    private SysUserLevelService sysUserLevelService;

    /**
     * 查询用户等级列表
     */
    @Operation(summary = "获取用户等级列表", description = "获取用户等级列表")
    @RequiresPermissions("user:level:list")
    @GetMapping("/list")
    public Result<DataPageResp<SysUserLevelPageResp>> list(SysUserLevelQueryReq sysUserLevelQueryReq) {
        PageQueryModel<SysUserLevelPageQueryModel> pageQueryModel = sysUserLevelService.pageQuery(SysUserLevelQueryModel.reqBuild(sysUserLevelQueryReq));
        return Result.success(DataPageResp.of(pageQueryModel).cover(SysUserLevelPageResp::infoBuild));
    }

    /**
     * 导出用户等级列表
     */
    @Operation(summary = "导出用户等级列表", description = "导出用户等级列表")
    @RequiresPermissions("user:level:export")
    @Log(title = "用户等级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysUserLevel sysUserLevel) {
        List<SysUserLevel> list = sysUserLevelService.exportList(sysUserLevel);
        ExcelUtil<SysUserLevel> util = new ExcelUtil<>(SysUserLevel.class);
        util.exportExcel(response, list, "用户等级数据");
    }

    /**
     * 获取用户等级详细信息
     */
    @Operation(summary = "获取用户等级详细信息", description = "获取用户等级详细信息")
    @RequiresPermissions("user:level:query")
    @GetMapping(value = "/{levelId}")
    public Result<SysUserLevel> getInfo(@PathVariable("levelId") Long levelId) {
        return Result.success(sysUserLevelService.getById(levelId));
    }

    /**
     * 获取用户等级下拉框选项
     */
    @Operation(summary = "获取用户等级下拉框选项", description = "获取用户等级下拉框选项")
    @GetMapping("/options")
    public Result<List<SysUserLevel>> getLevelOptions() {
        List<SysUserLevel> levels = sysUserLevelService.getLevelOptions();
        return Result.success(levels);
    }

    /**
     * 新增用户等级
     */
    @RequiresPermissions("user:level:add")
    @Log(title = "用户等级", businessType = BusinessType.INSERT)
    @PostMapping
    @Operation(summary = "新增用户等级", description = "新增用户等级")
    public Result<String> add(@RequestBody SysUserLevelSaveReq sysUserLevelSaveReq) {
        sysUserLevelService.save(SysUserLevelSaveModel.saveReqBuild(sysUserLevelSaveReq));
        return Result.success();
    }

    /**
     * 修改用户等级
     */
    @RequiresPermissions("user:level:edit")
    @Log(title = "用户等级", businessType = BusinessType.UPDATE)
    @PutMapping("/{levelId}")
    @Operation(summary = "修改用户等级", description = "修改用户等级")
    public Result<String> edit(@PathVariable(value = "levelId") Long levelId, @RequestBody SysUserLevelEditReq sysUserLevelEditReq) {
        sysUserLevelService.edit(SysUserLevelEditModel.editReqBuild(levelId,sysUserLevelEditReq));
        return Result.success();
    }

    /**
     * 删除用户等级
     */
    @RequiresPermissions("user:level:remove")
    @Log(title = "用户等级", businessType = BusinessType.DELETE)
	@DeleteMapping("/{levelIds}")
    @Operation(summary = "删除用户等级", description = "删除用户等级")
    public Result<String> remove(@PathVariable(value = "levelIds") List<Long> levelIds) {
        return toAjax(sysUserLevelService.removeBatchByIds(levelIds));
    }
}
